package com.monkeyboy.spring.utils;

import com.monkeyboy.spring.service.UserService;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @Author Gavin
 * @date 2021.02.21 11:02
 */
public class CglibUtil {
	public static Object getProxy() {
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(UserService.class);
		//MethodInterceptor就是程序员提供的代理逻辑，cglib会固定调用该类的intercept方法，这也是实现代理的主要逻辑
		enhancer.setCallback(new MethodInterceptor() {
			@Override
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
				System.out.println("aop-------");
				Object o1 = methodProxy.invokeSuper(method, objects);
				return o1;
			}
		});
		Object o = enhancer.create();
		return o;
	}
}
